//
//  XJSetNickNameViewController.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/12.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJSetNickNameViewController.h"

@interface XJSetNickNameViewController ()
@property(nonatomic, strong) UITextField *nameTextField; // 12个字符

@end

@implementation XJSetNickNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"个人设置";
    
    [self setupNav];
    [self setupView];
}

- (void)setupNav {
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:(UIBarButtonItemStylePlain) target:self action:@selector(savaButtonItemAction)];
}

- (void)savaButtonItemAction {
    
    NSString *name = self.nameTextField.text;
    
    if ([NSString isEmptyString:name]) {
        [JRToast showWithText:@"请输入昵称"];
        
        return;
    }
 
    // 取出空格之后在统计字符个数
    NSString *pureName = [NSString pureStrWithOriginStr:name];
    if (pureName.length > 12) {
        [JRToast showWithText:@"昵称长度12个字符以内"];
        return;
    }
    
    // 调接口
    
    [XJHttpTool requestEditPersonMessageWithName:pureName sex:nil avator:nil successH:^(id responseObjc) {
        NSNumber *code  = responseObjc[@"code"];
        if ([code isEqualToNumber:@0]) {
            [JRToast showWithText:@"修改成功"];
            
            NSDictionary * dic = responseObjc[@"data"][@"customer"];
            // 保存
            [[NSUserDefaults standardUserDefaults] setValue:dic[@"avatar"] forKey:@"avatar"];
            [[NSUserDefaults standardUserDefaults] setValue:dic[@"nickname"] forKey:@"nickname"];
            [[NSUserDefaults standardUserDefaults] setValue:dic[@"phone"] forKey:@"phone"];
            [[NSUserDefaults standardUserDefaults] setValue:dic[@"uuid"] forKey:@"uuid"];
            NSNumber *sex = dic[@"sex"];
            if ([sex isEqualToNumber:@1]) {
                [[NSUserDefaults standardUserDefaults] setValue:@"女" forKey:@"sex"];
            }else {
                [[NSUserDefaults standardUserDefaults] setValue:@"男" forKey:@"sex"];
            }
            
            [self.navigationController popViewControllerAnimated:YES];
        }else {
            
            [self nickNameHaveAlreadyExistAlert];
        }
    } failH:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}

- (void)setupView {
    CGFloat bgY = 64;
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(10, bgY, KWidth - 20, 50)];
    [self.view addSubview:bgView];
    
    UILabel *nickNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 50, 30)];
    nickNameLabel.text = @"昵称";
    [bgView addSubview:nickNameLabel];
    
    self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(100,10, KWidth - 100, 30)];
    self.nameTextField.placeholder = @"请输入昵称";
    self.nameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [bgView addSubview:self.nameTextField];
    
    // 线
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, bgView.frame.size.height - 1 ,bgView.frame.size.width, 0.5)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [bgView addSubview:lineView];
}


#pragma mark - 三 Private method
//昵称已存在弹框
- (void)nickNameHaveAlreadyExistAlert {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:@"昵称已经存在,请重新修改" preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
 
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        self.nameTextField.text = @"";
    }];
    
    [alertVC addAction:cancelAction];
    [alertVC addAction:sureAction];
    
    [self presentViewController:alertVC animated:YES completion:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
